package com.sz.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.BeanUtils;
import org.springframework.ui.ModelMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.diboot.core.vo.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.sz.mes.entity.CustomerType;
import com.sz.mes.dto.CustomerTypeDTO;
import com.sz.mes.vo.CustomerTypeListVO;
import com.sz.mes.vo.CustomerTypeDetailVO;
import com.sz.mes.service.CustomerTypeService;
import com.sz.mes.controller.BaseCustomCrudRestController;

import lombok.extern.slf4j.Slf4j;
import javax.validation.Valid;
import java.util.List;

/**
 * 客户类型 相关Controller
 *
 * @author hem
 * @version 1.0
 * @date 2023-04-04
 * Copyright © szhs
 */
@RestController
@RequestMapping("/customerType")
@BindPermission(name = "客户类型")
@Slf4j
public class CustomerTypeController extends BaseCustomCrudRestController<CustomerType> {
    @Autowired
    private CustomerTypeService customerTypeService;

    /**
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
//    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/list")
    public JsonResult getViewObjectListMapping(CustomerTypeDTO queryDto, Pagination pagination) throws Exception{
        QueryWrapper<CustomerType> queryWrapper = super.buildQueryWrapperByQueryParams(queryDto);
        List<CustomerTypeListVO> voList = customerTypeService.getViewObjectList(queryWrapper, null, CustomerTypeListVO.class);
        if (queryWrapper.isEmptyOfNormal()) {
            voList = BeanUtils.buildTree(voList);
        }
        return JsonResult.OK(voList);
    }

		/**
     * 获取树结构数据接口
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/tree")
    public JsonResult getRootNodeTree() throws Exception {
        List<CustomerTypeDetailVO> customerTypeVOList = customerTypeService.getCustomerTypeTree(0L);
        return JsonResult.OK(customerTypeVOList);
    }

    /**
     * 根据资源id查询ViewObject
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
//    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id")Long id) throws Exception{
        return super.getViewObject(id, CustomerTypeDetailVO.class);
    }

    /**
     * 创建资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
//    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/")
    public JsonResult createEntityMapping(@Valid @RequestBody CustomerType entity) throws Exception {
        return super.createEntity(entity);
    }

    /**
     * 根据ID更新资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
//    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult updateEntityMapping(@PathVariable("id")Long id, @Valid @RequestBody CustomerType entity) throws Exception {
        return super.updateEntity(id, entity);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
//    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult deleteEntityMapping(@PathVariable("id")Long id) throws Exception {
        return super.deleteEntity(id);
    }

		/**
     * 删除对象前的操作
     *
     * @param entityOrDto
     * @return
     * @throws Exception
     */
    protected String beforeDelete(CustomerType entityOrDto) throws Exception {
        // 判断是否具有叶子节点（仅允许没有叶子节点的节点进行删除操作）
        LambdaQueryWrapper<CustomerType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerType::getParentId, entityOrDto.getPrimaryKeyVal());
        Long count = customerTypeService.getEntityListCount(wrapper);
        if (count > 0) {
            return "请先删除当前节点下的所有下级节点再重试";
        }
        return null;
    }

}
